<?php
$db_server = $argv[1];
$db_user = $argv[2];
$db_pwd = $argv[3];
if ($db_pwd == 'no-pwd') $db_pwd = '';
$db_name = $argv[4];
$file_name = $argv[5];
if (empty($file_name)) {
   $file_name = 'dbimage.csv';
}

echo "$db_server, $db_user, $db_pwd, $db_name, $file_name.\n";
if (!$fout = fopen($file_name, 'w')) {
   die("impossibile creare il file $file_name\n");
}

if (!$conn = mysql_connect($db_server, $db_user, $db_pwd)) {
   die(mysql_error());
}
if (!$db = mysql_select_db($db_name)) {
   die(mysql_error());
}
$sql = "SHOW TABLE STATUS";
if (!$qryt = mysql_query($sql)) {
   die($sql . ' - ' . mysql_error());
}
while ($rect = mysql_fetch_assoc($qryt)) {
   $tab = $rect['Name'];
	$sout  = '"'.$tab.'";"tab';
	if (substr($rect['Comment'],0,6)== "InnoDB") $aX[0] = "";
   else $aX = explode("; InnoDB free:",$rect['Comment']);     
   $commX = $aX[0];
   $sout .= '";"";"";"";"";"";"'.$commX;
	$sout .= '"'."\r\n";
	fputs($fout, $sout);
   $adb = array();
   $sql = "SHOW FULL COLUMNS FROM $tab";
   if (!$qryc = mysql_query($sql)) {
      die($sql . ' - ' . mysql_error());
   }
   $sout = "$tab";
   while ($recc = mysql_fetch_assoc($qryc)) {
      $type = $recc['Type'];
      if (substr($type,0,6)=='varbin') {
         $type = 'varchar'.substr($type,6).' binary';
      } elseif (isset($recc['Collation']) and $recc['Collation'] == 'latin1_bin') {
				if (substr($type,-7) != ' binary') {
					$type .= ' binary';
				}
      }
		$nullable = ($recc['Null'] == 'YES')?'YES':'';	
      $sout  = '"'.$tab.'";"col';
      $sout .= '";"'.$recc['Field'];
      $sout .= '";"'.$type;
      $sout .= '";"'.$nullable;
      $sout .= '";"'.$recc['Default'] ;
      $sout .= '";"'.$recc['Extra'];
      $sout .= '";"'.$recc['Comment'];
      $sout .= '"'."\r\n";
      fputs($fout, $sout);
   }
   $sql = "SHOW INDEX FROM $tab";
   if (!$qryc = mysql_query($sql)) {
      die($sql . ' - ' . mysql_error());
   }
   $sout = "$tab";
   while ($recc = mysql_fetch_assoc($qryc)) {
      $sout = '"'.$tab.'";"ind';
      $sout .= '";"'.$recc['Key_name'];
      $sout .= '";"'.$recc['Seq_in_index'];
      $sout .= '";"'.$recc['Column_name'];
      if ($recc['Index_type'] == 'FULLTEXT') {
         $sout .= '";"'.$recc['Index_type'];
      } else {
         $sout .= '";"'.$recc['Comment'];
      }
      $sout .= '"'."\r\n";
      fputs($fout, $sout);
   }
}
fclose($fout);
mysql_close($conn);

?>